package DB;

import java.sql.Connection;
import POJO.ContentObject;
import java.sql.ResultSet;
import java.util.Vector;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.regex.*;

public class ContentManager extends DB {

    public ContentManager(String dbName, String userName, String password) {
        super(dbName, userName, password);
    }

    public ContentManager(Connection c) {
        conn = c;
    }

    public Vector<ContentObject> getImages(int containerID) {
        Vector<ContentObject> output = new Vector<ContentObject>();

        if (isConnected()) {
            try {
                PreparedStatement pstmt = conn.prepareStatement("SELECT contentID, contentLink FROM Contents WHERE containerID= ? AND type='IMAGE'");
                pstmt.setInt(1, containerID);
                ResultSet rs = pstmt.executeQuery();

                while (rs.next()) {
                    output.add(new ContentObject(rs.getInt("contentID"), rs.getString("contentLink")));
                }
            } catch (SQLException sql) {
                return output;
            }
        }
        return output;
    }

    public Vector<ContentObject> getBlogposts(int containerID) {
        Vector<ContentObject> output = new Vector<ContentObject>();

        if (isConnected()) {
            try {
                PreparedStatement pstmt = conn.prepareStatement("SELECT contentID, contentLink FROM Contents WHERE containerID= ? AND type='BLOGPOST'");
                pstmt.setInt(1, containerID);
                ResultSet rs = pstmt.executeQuery();

                while (rs.next()) {
                    output.add(new ContentObject(rs.getInt("contentID"), rs.getString("contentLink")));
                }
            } catch (SQLException sql) {
                return output;
            }
        }
        return output;
    }

    public Vector<ContentObject> getVideos(int containerID) {
        Vector<ContentObject> output = new Vector<ContentObject>();

        if (isConnected()) {
            try {
                PreparedStatement pstmt = conn.prepareStatement("SELECT contentID, contentLink FROM Contents WHERE containerID= ? AND type='VIDEO'");
                pstmt.setInt(1, containerID);
                ResultSet rs = pstmt.executeQuery();

                while (rs.next()) {
                    String videolink = rs.getString("contentLink");

                    // Compile regular expression
                    Pattern pattern = Pattern.compile("watch\\?v=");

                    // Replace all occurrences of pattern in input
                    Matcher matcher = pattern.matcher(videolink);
                    videolink = matcher.replaceAll("v/");

                    output.add(new ContentObject(rs.getInt("contentID"), videolink));
                }
            } catch (SQLException sql) {
                return output;
            }
        }
        return output;
    }

    public boolean addVideo(String ytlink, int container) {
        if (isConnected()) {
            try {
                PreparedStatement pstmt = conn.prepareStatement("INSERT INTO Contents (contentLink, containerID, type) VALUES (?,?, 'VIDEO')");
                pstmt.setString(1, ytlink);
                pstmt.setInt(2, container);
                pstmt.executeUpdate();
                return true;
            } catch (SQLException sql) {
                return false;
            }
        } else {
            return false;
        }
    }

    public boolean addBlogpost(String blogpost, int container) {
        if (isConnected()) {
            try {
                PreparedStatement pstmt = conn.prepareStatement("INSERT INTO Contents (contentLink, containerID, type) VALUES (?,?, 'BLOGPOST')");
                pstmt.setString(1, blogpost);
                pstmt.setInt(2, container);
                pstmt.executeUpdate();
                return true;
            } catch (SQLException sql) {
                return false;
            }
        } else {
            return false;
        }
    }

    public boolean changeBlogpost(String blogpost, int id) {
        if (isConnected()) {
            try {
                PreparedStatement pstmt = conn.prepareStatement("UPDATE Contents SET contentLink = ? WHERE contentId = ? AND type = 'BLOGPOST'");
                pstmt.setString(1, blogpost);
                pstmt.setInt(2, id);
                pstmt.executeUpdate();
                return true;
            } catch (SQLException sql) {
                System.out.println("Change blogpost failed: " + sql.getMessage());
                return false;
            }
        } else {
            return false;
        }
    }

    public int getContainerID(int contentID) {
        if (isConnected()) {
            try {
                PreparedStatement pstmt = conn.prepareStatement("SELECT containerID FROM Contents WHERE contentID= ?");
                pstmt.setInt(1, contentID);
                ResultSet rs = pstmt.executeQuery();

                if (rs.next()) {
                    return rs.getInt(1);
                } else {
                    return -1;
                }
            } catch (SQLException sql) {
                return -1;
            }
        }
        return -1;
    }

    public void removeObject(int objectID) {
        if (isConnected()) {
            try {
                PreparedStatement pstmt = conn.prepareStatement("DELETE FROM Contents WHERE contentID= ?");
                pstmt.setInt(1, objectID);
                pstmt.executeUpdate();
            } catch (SQLException sql) {
            }
        }
    }

    public int getLatestContendID() {
        if (isConnected()) {
            try {
                PreparedStatement pstmt = conn.prepareStatement("SELECT MAX(contentID) FROM Contents");
                ResultSet rs = pstmt.executeQuery();

                if (rs.next()) {
                    return rs.getInt(1);
                } else {
                    return -1;
                }
            } catch (SQLException sql) {
                return -1;
            }
        }
        return -1;
    }

    public boolean addImage(String image, int container) {
        if (isConnected()) {
            try {
                PreparedStatement pstmt = conn.prepareStatement("INSERT INTO Contents (contentLink, containerID, type) VALUES (?,?, 'IMAGE')");
                pstmt.setString(1, image);
                pstmt.setInt(2, container);
                pstmt.executeUpdate();
                return true;
            } catch (SQLException sql) {
                return false;
            }
        } else {
            return false;
        }
    }

    public String getContentType(int contentID) {
        if (isConnected()) {
            try {
                PreparedStatement pstmt = conn.prepareStatement("SELECT type FROM Contents WHERE contentID= ?");
                pstmt.setInt(1, contentID);
                ResultSet rs = pstmt.executeQuery();

                if (rs.next()) {
                    return rs.getString("type");
                } else {
                    return null;
                }
            } catch (SQLException sql) {
                return null;
            }
        }
        return null;
    }

    public String getContentLink(int contentID) {
        if (isConnected()) {
            try {
                PreparedStatement pstmt = conn.prepareStatement("SELECT contentLink FROM Contents WHERE contentID= ?");
                pstmt.setInt(1, contentID);
                ResultSet rs = pstmt.executeQuery();

                if (rs.next()) {
                    return rs.getString("contentLink");
                } else {
                    return null;
                }
            } catch (SQLException sql) {
                return null;
            }
        }
        return null;
    }
}


